package com.gewu.pm.ui.adapter.home;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.TestBean;

/**
 * 客户拜访报表适配器
 */
public class CustomerVisitReportAdapter extends AppAdapter<TestBean> {

    public CustomerVisitReportAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder();
    }

    private final class ItemViewHolder extends AppAdapter<TestBean>.ViewHolder {

        private final FrameLayout mRankContainer;
        private final ImageView mRankIcon;
        private final TextView mRankNumber;
        private final TextView mNameView;

        private ItemViewHolder() {
            super(R.layout.customer_visit_report_item);
            mRankContainer = findViewById(R.id.fl_rank_container);
            mRankIcon = findViewById(R.id.iv_rank_icon);
            mRankNumber = findViewById(R.id.tv_rank_number);
            mNameView = findViewById(R.id.tv_name);
        }

        @Override
        public void onBindView(int position) {
            TestBean item = getItem(position);
            
            // 设置名称
            mNameView.setText(item.getName());
            
            // 设置排名显示
            setupRankDisplay(position + 1);
        }

        /**
         * 设置排名显示
         * @param rank 排名（1-based）
         */
        private void setupRankDisplay(int rank) {
            // 重置显示状态
            mRankIcon.setVisibility(GONE);
            mRankNumber.setVisibility(GONE);
            
            if (rank == 1) {
                // 第一名显示金冠
                mRankIcon.setImageResource(R.drawable.crown_one);
                mRankIcon.setVisibility(VISIBLE);
            } else if (rank == 2) {
                // 第二名显示银冠
                mRankIcon.setImageResource(R.drawable.crown_two);
                mRankIcon.setVisibility(VISIBLE);
            } else if (rank == 3) {
                // 第三名显示铜冠
                mRankIcon.setImageResource(R.drawable.crown_three);
                mRankIcon.setVisibility(VISIBLE);
            } else {
                // 其他排名显示数字圆圈
                String displayRank = rank > 99 ? "99+" : String.valueOf(rank);
                mRankNumber.setText(displayRank);
                mRankNumber.setVisibility(VISIBLE);
            }
        }
    }
}
